package com.example.mytext;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecordButton texta;
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int REQUEST_CODE = 1024;
    int CAMERA_OK = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        texta = findViewById(R.id.texta);
        request_permissions();
        texta.setRecordButtonListener(new RecordButton.RecordButtonListener() {
            @Override
            public void onClick() {
                Log.e(TAG, "onClick: ");
                Toast.makeText(MainActivity.this, "点击了按钮", Toast.LENGTH_SHORT).show();
                Intent in = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(in);
            }
            @Override
            public void onLongClick() {
                Log.e(TAG, "onLongClick: ");
                Toast.makeText(MainActivity.this, "长按了按钮", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClickFinish(int result) {
                Log.e(TAG, "onLongClickFinish: " + result);
                switch (result) {
                    case RecordButton.NORMAL:
                        Toast.makeText(MainActivity.this, "长按结束", Toast.LENGTH_SHORT).show();
                        break;
                    case RecordButton.RECORD_SHORT:
                        Toast.makeText(MainActivity.this, "录制时间过短", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                }
            }
        });
        /**
         * 这三个属性也可以在xml文件中设置
         */
        //设置触摸延迟时间，区分点击还是长按，单位是毫秒
        texta.setTouchDelay(300);
        //设置最大录制时间，单位为毫秒
        texta.setRecordTime(50000);
        //设置最小录制时间，单位为毫秒
        texta.setMinRecordTime(1000);
    }


    // 请求多个权限
    private void request_permissions() {
        // 创建一个权限列表，把需要使用而没用授权的的权限存放在这里
        List<String> permissionList = new ArrayList<>();

        // 判断权限是否已经授予，没有就把该权限添加到列表中
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.CAMERA);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO)!=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.RECORD_AUDIO);
        }
        // 如果列表为空，就是全部权限都获取了，不用再次获取了。不为空就去申请权限
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions(this,
                    permissionList.toArray(new String[permissionList.size()]), 1002);
        } else {
            Toast.makeText(this, "多个权限你都有了，不用再次申请", Toast.LENGTH_LONG).show();
        }
    }
    // 请求权限回调方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1002:
                // 1002请求码对应的是申请多个权限
                if (grantResults.length > 0) {
                    // 因为是多个权限，所以需要一个循环获取每个权限的获取情况
                    for (int i = 0; i < grantResults.length; i++) {
                        // PERMISSION_DENIED 这个值代表是没有授权，我们可以把被拒绝授权的权限显示出来
                        if (grantResults[i] == PackageManager.PERMISSION_DENIED){
                            Toast.makeText(MainActivity.this, permissions[i] + "权限被拒绝了", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                break;
        }
    }






    //申请录音权限
    private static final int GET_RECODE_AUDIO = 1;
    private static String[] PERMISSION_AUDIO = {
            Manifest.permission.RECORD_AUDIO
    };

    /*
     * 申请录音权限*/
    public static void verifyAudioPermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, PERMISSION_AUDIO, GET_RECODE_AUDIO);
        }
    }
    private void getPermission() {
        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //先判断有没有权限 ，没有就在这里进行权限的申请
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_OK);
            } else {
                //说明已经获取到摄像头权限了
                Log.i("MainActivity", "已经获取了权限");
            }
        } else {
//这个说明系统版本在6.0之下，不需要动态获取权限。
            Log.i("MainActivity", "这个说明系统版本在6.0之下，不需要动态获取权限。");
        }
    }
}